﻿using Business;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace BVVD_Website.userRight
{
    public partial class frmGroup_Menu1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                grvGroup_User.Culture = new CultureInfo("vi-VN");
                grvGroup_User.Rebind();

                BVVDDataContext db = new BVVDDataContext();

                var listNHOM = (from lh in db.LST_NHOMNSDs
                                where lh.TRANGTHAI.Equals("1")
                                select new
                                {
                                    MANHOM = lh.MANHOM,
                                    TENNHOM = lh.TENNHOM
                                }).ToList();

                ddlGROUP.DataSource = listNHOM.OrderBy(a => a.MANHOM);
                ddlGROUP.DataTextField = "TENNHOM";
                ddlGROUP.DataValueField = "MANHOM";
                ddlGROUP.DataBind();
                fillgrvGroup_Menu();
                grvGroup_User.DataBind();

            }
        }

        protected void grvGroup_User_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            fillgrvGroup_Menu();

        }

        private void fillgrvGroup_Menu()
        {
            BVVDDataContext db = new BVVDDataContext();

            var list = (from mn in db.LST_MENUs
                        where mn.TYPE == 2 && mn.LEVEL == 2
                        join gr in db.F_NHOM_MENUs on new { MAMN = mn.MAMN, MANHOM = ddlGROUP.SelectedValue } equals new { MAMN = gr.MAMN, MANHOM = gr.MANHOM }
                        into temp1
                        from tmp1 in temp1.DefaultIfEmpty()

                        select new
                        {
                            RECID = mn.MAMN,
                            MAMN = mn.MAMN,
                            TENMN = mn.TEN,
                            MANHOM = tmp1.MANHOM == null ? "" : tmp1.MANHOM,
                            TENNHOM = tmp1.LST_NHOMNSD.TENNHOM,
                            SHOW = tmp1.SHOW != null ? (tmp1.SHOW.ToString().Equals("1") ? 1 : 0) : 0,
                            ADD = tmp1.ADD != null ? (tmp1.ADD.ToString().Equals("1") ? 1 : 0) : 0,
                            EDIT = tmp1.EDIT != null ? (tmp1.EDIT.ToString().Equals("1") ? 1 : 0) : 0,
                            DELETE = tmp1.DELETE != null ? (tmp1.DELETE.ToString().Equals("1") ? 1 : 0) : 0
                        }).ToList();

            if (ddlGROUP.SelectedIndex >= 0)
            {
                if (list.Where(a => a.MANHOM.Equals(ddlGROUP.SelectedValue)).Count() > 0)
                {
                    grvGroup_User.DataSource = list.Where(a => a.MANHOM.Equals(ddlGROUP.SelectedValue));
                }
                else
                {
                    grvGroup_User.DataSource = list;
                }//.Union(list.Where(a => !a.MANHOM.Equals(ddlGROUP.SelectedValue)));
            }
            else
            {
                grvGroup_User.DataSource = list.Where(a => a.RECID.Equals("N/A"));
            }

        }

        protected void ddlGROUP_SelectedIndexChanged(object sender, Telerik.Web.UI.DropDownListEventArgs e)
        {
            fillgrvGroup_Menu();
            grvGroup_User.DataBind();
        }

        protected void grvGroup_User_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName.Equals("SavePQ"))
            {
                BVVDDataContext db = new BVVDDataContext();
                foreach (GridDataItem dr in grvGroup_User.Items)
                {
                    string mamn = dr["MAMN"].Text;
                    string manhom = ddlGROUP.SelectedValue;
                    int show = (dr.FindControl("chbShow") as CheckBox).Checked ? 1 : 0;
                    int add = (dr.FindControl("chbAdd") as CheckBox).Checked ? 1 : 0;
                    int edit = (dr.FindControl("chbEdit") as CheckBox).Checked ? 1 : 0;
                    int delete = (dr.FindControl("chbDelete") as CheckBox).Checked ? 1 : 0;

                    F_NHOM_MENU obj = new F_NHOM_MENU
                    {
                        MANHOM = manhom,
                        MAMN = mamn,
                        SHOW = show,
                        ADD = add,
                        EDIT = edit,
                        DELETE = delete,
                        NGAYGAN = DateTime.Now,
                        NGUOIGAN = Session["user"] != null ? (Session["user"] as LST_NSD).MANSD : ""
                    };
                    checkRowExist(obj);


                }
                grvGroup_User.Rebind();
                rnAlert.Text = "Lưu thông tin thành công ";
                rnAlert.Show();
            }
        }

        private string checkRowExist(F_NHOM_MENU fc)
        {
            BVVDDataContext db = new BVVDDataContext();
            F_NHOM_MENU value = (from ck in db.F_NHOM_MENUs
                                 where ck.MAMN.Equals(fc.MAMN) && ck.MANHOM.Equals(fc.MANHOM)
                                 select ck).SingleOrDefault();
            if (value == null)
            {
                db.F_NHOM_MENUs.InsertOnSubmit(fc);
                db.SubmitChanges();
                return "Add";
            }
            else if (!value.SHOW.ToString().Equals(fc.SHOW.ToString()) || !value.ADD.ToString().Equals(fc.ADD.ToString())
                || !value.EDIT.ToString().Equals(fc.EDIT.ToString()) || !value.DELETE.ToString().Equals(fc.DELETE.ToString())
                )
            {
                value.ADD = fc.ADD;
                value.SHOW = fc.SHOW;
                value.EDIT = fc.EDIT;
                value.DELETE = fc.DELETE;
                value.NGUOIGAN = Session["user"] != null ? (Session["user"] as LST_NSD).MANSD : fc.NGUOIGAN;
                value.NGAYGAN = DateTime.Now;
                db.SubmitChanges();
                return "Update";
            }
            else
            {

                return "Cancel";
            }

        }
    }
}